//package com.Lyf.user.util;
//
//import com.Lyf.user.config.ElasticSearchConstant;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//
//import jakarta.annotation.Resource;
//import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.delete.DeleteResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.support.master.AcknowledgedResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
//import org.elasticsearch.client.indices.CreateIndexResponse;
//import org.elasticsearch.client.indices.GetIndexRequest;
//
//import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.core.TimeValue;
//import org.elasticsearch.xcontent.XContentType;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.io.IOException;
//
//public class ESUtil {
//
//    @Resource
//    private static RestHighLevelClient restHighLevelClient;
//
//    /**
//     * 判断索引是否存在
//     * @param indexName
//     * @return
//     */
//    public static boolean isIndexExist(String indexName) {
//        GetIndexRequest request = new GetIndexRequest(indexName);
//        try {
//            boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
//            return exists;
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 创建索引
//     * @param indexName
//     * @return
//     */
//    public static boolean createIndex(String indexName)
//    {
//        if(isIndexExist(indexName)) {
//            return false;
//        }
//        //创建索引
//        CreateIndexRequest request = new CreateIndexRequest(indexName);
//        //参数配置
//        request.settings(
//                //分片数
//                Settings.builder().put("index.number_of_shards", ElasticSearchConstant.INDEX_NUMBER_OF_SHARDS)
//                        //备份数
//                        .put("index.number_of_replicas", ElasticSearchConstant.INDEX_NUMBER_OF_REPLICAS)
//        );
//        try {
//            CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
//            return response.isAcknowledged();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 删除索引
//     * @param indexName
//     * @return
//     */
//    public boolean deleteIndex(String indexName)
//    {
//        if(isIndexExist(indexName)) {
//            return false;
//        }
//        DeleteIndexRequest request = new DeleteIndexRequest(indexName);
//        try {
//            AcknowledgedResponse delete = restHighLevelClient.indices().delete(request,RequestOptions.DEFAULT);
//            return delete.isAcknowledged();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 向索引中添加数据
//     * @param jsonObject  json字符串
//     * @param indexName 索引名称
//     * @param id 数据id
//     * @return
//     */
//    public static String addDoc(JSONObject jsonObject, String indexName,String id)
//    {
//        IndexRequest indexRequest = new IndexRequest(indexName);
//        indexRequest.id(id);
//        IndexRequest source = indexRequest.source(jsonObject, XContentType.JSON);
//        //超时时间
//        indexRequest.timeout(TimeValue.timeValueSeconds(1));
//        //客户端发送请求
//        try {
//            IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
//            return response.getId();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//    }
//
//    /**
//     * 通过docId删除数据
//     * @param indexName
//     * @param id
//     */
//    public static void deleteDocById(String indexName,String id)
//    {
//        //删除请求
//        DeleteRequest request = new DeleteRequest(indexName, id);
//        //执行客户端请求
//        try {
//            DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 更新数据
//     * @param object
//     * @param indexName
//     * @param id
//     */
//    public static void updateDataById(Object object,String indexName,String id)
//    {
//        UpdateRequest updateRequest = new UpdateRequest(indexName, id);
//
//        updateRequest.timeout("1s");
//        updateRequest.doc(JSON.toJSONString(object),XContentType.JSON);
//        //执行更新请求
//        try {
//            UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 实时更新数据
//     * @param object
//     * @param indexName
//     * @param id
//     */
//    public static void updateDataByIdNoRealTime(Object object,String indexName,String id)
//    {
//        //更新请求
//        UpdateRequest updateRequest = new UpdateRequest(indexName, id);
//        //保障数据实时更新(无非就是新增参数)
//        updateRequest.setRefreshPolicy("wait_for");
//        updateRequest.timeout("1s");
//        updateRequest.doc(JSON.toJSONString(object),XContentType.JSON);
//        //执行更新请求
//        try {
//            UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//
//
//}
